//
//  M1LocationTransform.m
//  NewMarryU
//
//  Created by apple on 2018/7/24.
//  Copyright © 2018年 Cedar. All rights reserved.
//

#import "M1LocationTransform.h"
#import <CoreLocation/CoreLocation.h>
@implementation M1LocationTransform
const double a = 6378245.0;
const double m1_e = 0.00669342162296594323;
+ (CLLocationCoordinate2D)m1_locationTransform:(CLLocationCoordinate2D)coor{
    double m1_wLat = coor.latitude;
    double m1_wLon = coor.longitude;
    double m1_gLat;
    double m1_gLon;
    if ([self m1_outOfChina:m1_wLat :m1_wLon ])
    {
        return coor;
    }
    double m1_dLat = [self m1_transformLat:m1_wLon-105.0 :m1_wLat - 35 ];
    double m1_dLon = [self m1_transformLon:m1_wLon-105.0 :m1_wLat - 35 ];
    double radLat = m1_wLat / 180.0 * M_PI;
    double magic = sin(radLat);
    magic = 1 - m1_e * magic * magic;
    double sqrtMagic = sqrt(magic);
    m1_dLat = (m1_dLat * 180.0) / ((a * (1 - m1_e)) / (magic * sqrtMagic) * M_PI);
    m1_dLon = (m1_dLon * 180.0) / (a / sqrtMagic * cos(radLat) * M_PI);
    m1_gLat = m1_wLat + m1_dLat;
    m1_gLon = m1_wLon + m1_dLon;
    CLLocationCoordinate2D loc2D ;
    loc2D.latitude = m1_gLat;
    loc2D.longitude = m1_gLon;
    return loc2D;
}
+ (BOOL) m1_outOfChina:(double)lat :(double)lon
{
    if (lon < 72.004 || lon > 137.8347) {
        return true;
    }
    if (lat < 0.8293 || lat > 55.8271) {
        return true;
    }
    return false;
}
+ (double) m1_transformLat:(double)x  :(double)y
{
    double m1_ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y +
    0.2 * sqrt(fabs(x));
    m1_ret += (20.0 * sin(6.0 * x * M_PI) + 20.0 *sin(2.0 * x *M_PI)) * 2.0 /
    3.0;
    m1_ret += (20.0 * sin(y * M_PI) + 40.0 *sin(y / 3.0 *M_PI)) * 2.0 / 3.0;
    m1_ret += (160.0 * sin(y / 12.0 * M_PI) + 320 *sin(y * M_PI / 30.0)) * 2.0 /
    3.0;
    return m1_ret;
}
+ (double) m1_transformLon:(double) x :(double) y
{
    double m1_ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(fabs(x));
    m1_ret += (20.0 * sin(6.0 * x * M_PI) + 20.0 * sin(2.0 * x * M_PI)) * 2.0 /
    3.0;
    m1_ret += (20.0 * sin(x * M_PI) + 40.0 * sin(x / 3.0 * M_PI)) * 2.0 / 3.0;
    m1_ret += (150.0 * sin(x / 12.0 *M_PI) + 300.0 *sin(x / 30.0 * M_PI)) * 2.0 /
    3.0;
    return m1_ret;
}
@end
